package com.product.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.product.mapper.DishFlavorMapper;
import com.product.mapper.DishMapper;
import com.product.mapper.SetmealMapper;
import com.product.pojo.dto.DishDTO;
import com.product.pojo.dto.DishPageQueryDTO;
import com.product.pojo.entity.Dish;
import com.product.pojo.entity.DishFlavor;
import com.product.pojo.vo.DishVO;
import com.product.service.DishService;
import com.sky.constant.MessageConstant;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.result.PageResult;
import com.sky.result.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    DishMapper dishMapper;

    @Autowired
    DishFlavorMapper dishFlavorMapper;

    @Autowired
    SetmealMapper setmealMapper;
    public DishServiceImpl(DishMapper dishMapper) {
        this.dishMapper = dishMapper;
    }
    /**
     * 新增菜品业务接口
     * @param
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDTO dishDto) {
        //复制获取属性
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDto,dish);
        //向菜品表插入一条数据
        dishMapper.insert(dish);
        //通过主键回显获取菜品id
        Long id=dish.getId();
        //循环lamba赋值
        //向口味表插入多条数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        if(flavors!=null&&flavors.size()>0){
            flavors.forEach(flavor->{flavor.setDishId(id);});
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 分页查询菜品
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        //获取页码值和页容量
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        //调用select方法查询dish表
        //page extends arrayList
        Page<DishVO> page=dishMapper.queryPage(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据传入的id列表批量删除
     * @param dishIds
     */
    @Override
    @Transactional
    public void deleteBatch(List<Long> dishIds) {
           //判断该菜品是否处于售卖状态
           for(Long id : dishIds){
           //遍历每一个菜品id，根据id去查询id对应的状态
               Dish dish=dishMapper.getById(id);
               if(dish.getStatus()==1){
                   //该菜品处于出售状态，不能删除
                   throw  new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
               }
           }
           //判断菜品id是否关联某个套餐
           List<Long> list= setmealMapper.getByDishIds(dishIds);
           if(list!=null&&list.size()>0){
                   throw  new DeletionNotAllowedException (MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
           }
           //删除该菜品
           //for(Long dishId : dishIds){
               //dishMapper.deleteById(dishId);
               ////判断该菜品id对应的口味表是否删除
               //dishFlavorMapper.deleteByDishId(dishId);
           //}
           //TODO:一个批量删除的优化
           //批量删除菜品
           dishMapper.deleteByDishIds(dishIds);
           //批量删除口味表
           dishFlavorMapper.deleteByDishIds(dishIds);

    }

    /**
     * id查询dish和flavor
     * @param id
     * @return
     */
    @Override
    public DishVO getDishWithFlavorById(Long id) {
        Dish dish=dishMapper.getById(id);
        DishVO dishVO=new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        List<DishFlavor> list=dishFlavorMapper.getByDishId(id);
        dishVO.setFlavors(list);
        return dishVO;
    }

    /**
     * 修改菜品表
     * @param dishDTO
     */
    @Override
    public void updateDishWithFlavor(DishDTO dishDTO) {
         //修改菜品基本表
         Dish dish=new Dish();
         BeanUtils.copyProperties(dishDTO,dish);
         dishMapper.update(dish);
         //修改口味表
         //1.先根据id删除原有的口味表
         dishFlavorMapper.deleteByDishId(dishDTO.getId());
         //2.将修改后的的数据插入
         List<DishFlavor> flavors=dishDTO.getFlavors();
         //如果表不为空
         if(flavors!=null&&flavors.size()>0){
             //设置口味表的id
             flavors.forEach(flavor->{flavor.setDishId(dish.getId());});//写成lamba表达式
             dishFlavorMapper.insertBatch(flavors);
         }
    }

    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        /**
         * 条件查询菜品和口味
         * @param dish
         * @return
         */
        //list原先就已经存在
            //这里的dish只封装了status 和分类id
            List<Dish> dishList = dishMapper.list(dish);
            List<DishVO> dishVOList = new ArrayList<>();
            for (Dish d : dishList) {
                DishVO dishVO = new DishVO();
                BeanUtils.copyProperties(d,dishVO);
                //根据菜品id查询对应的口味
                List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());
                dishVO.setFlavors(flavors);
                dishVOList.add(dishVO);
            }

            return dishVOList;
        }

    /**
     * 根据id修改
     * @param id
     */
    @Override
    public void updateStatusById(Integer status,Integer id) {
            System.out.println("ddsa"+status+":"+id);
           dishMapper.updateStatusById(status,id);
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> listByCategoryId(String categoryId) {
        List<Dish> dishList=dishMapper.listByCategoryId(categoryId);
        return dishList;
    }

    /**
     * 根据map统计菜品
     * @param map
     * @return
     */
    @Override
    public Result<Integer> countByMap(Map<String, Object> map) {
        Integer count = dishMapper.countByMap(map);
        if(count==null) return Result.error("未知错误");
        return Result.success(count);
    }

}
